<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 会员 model
 *
 */
class Usermodel extends CI_Model {
	function __construct()
    {
        parent::__construct();
    }

    /**
     * 根据会员id查询是否其是否存在
     *
     * @param unknown_type $id
     * @return unknown
     */
	function check_user_by_id($id='')
	{
		$this->db->select('count(*) as count');
		$query = $this->db->get_where('user', array('id' => $id));
		$row = $query->row_array();
		return $row['count'] > 0;
	}
	
	/**
	 * 根据用户名查询是否其是否存在
	 *
	 * @param unknown_type $name
	 * @return unknown
	 */
	function check_user_by_username($name='', $uid='')
	{
		$where = array('name' => $name);
		$uid && $where['id <>'] = $uid;
		$this->db->select('count(*) as count');
		$query = $this->db->get_where('user', $where);
		$row = $query->row_array();
		return $row['count'] > 0;
	}
	
	/**
	 * 根据token查找uid
	 *
	 * @param unknown_type $token
	 * @return unknown
	 */
	function get_id_by_token($token='')
	{
		$this->db->select('id');
		$query = $this->db->get_where('user', array('token' => $token));
		$row = $query->row_array();
		if($row)
			return $row['id'];
		else 
			return false;
	}
	
	/**
	 * 验证邮件地址是否重复
	 *
	 * @param unknown_type $email
	 */
	function check_email($email='', $uid='')
	{
		$where = array('email' => $email);
		$uid && $where['id <>'] = $uid;
		$this->db->select('count(*) as count');
		$query = $this->db->get_where('user', $where);
		$row = $query->row_array();
		return $row['count'] > 0;
	}
	
	/**
	 * 添加用户
	 *
	 * @param unknown_type $data
	 */
	function insert_user($data=array())
	{
		if (empty($data) || !isset($data['email']) || empty($data['email']) ||
				!isset($data['password']) || empty($data['password'])) {
			return false;
		}
		//插入
		$this->db->set('id', '(SELECT UUID())', FALSE);
		$this->db->set('createtime', '(NOW())', FALSE);
		return $this->db->insert('user', $data);
	}
	
	/**
	 * 合作账号注册
	 *
	 */
	function insert_user_join($data=array())
	{
		if(empty($data) || !isset($data['id']) || empty($data['id'])){
			return false;
		}
		$this->db->set('createtime', '(NOW())', FALSE);
		return $this->db->insert('user', $data);
	}
	
	/**
	 * 获取会员信息
	 *
	 * @param unknown_type $id
	 * @return unknown
	 */
	function get_userinfo($id='')
	{
		$query = $this->db->get_where('user', array('id'=>$id));
		return $query->result_array();
	}
	
	/**
	 * 根据email查找用户信息
	 *
	 * @param unknown_type $email
	 * @return unknown
	 */
	function get_userinfo_by_email($email='')
	{
		$this->db->limit(1);
		$query = $this->db->get_where('user', array('email'=>$email));
		return $query->result_array();
	}
	
	/**
	 * 登录检测，返回用户id
	 *
	 * @param unknown_type $username	既可表示name也可表示email
	 * @param unknown_type $password
	 * @return unknown
	 */
	function check_login($username='', $password='')
	{
		$password = md5($password);
		$this->db->select('id, email, name, token')
					->from('user')
					->where("(`email`='$username' AND `password`='$password') OR (`name`='$username' AND `password`='$password')")
					->limit(1);
		$query = $this->db->get();
		if ($row = $query->row_array()) {
			return $row;
		}	
		return false;
	}
	
	/**
	 * 更新用户昵称
	 *
	 * @param unknown_type $name
	 */
	function update_name($uid='', $name='')
	{
		if(empty($name) || empty($uid)) return FALSE;
		$this->db->set('name', $name);
		return $this->db->update('user', null, array('id' => $uid));
	}
	
	/**
	 * 更新会员图像
	 *
	 * @param unknown_type $uid
	 * @param unknown_type $photo
	 * @return unknown
	 */
	function update_photo($uid='', $photo='')
	{
		if (empty($uid) || empty($photo)) return FALSE;
		//$this->db->set('photo', $photo);
		return $this->db->update('user', array('photo' => $photo), array('id' => $uid));
	}
	
	/**
	 * 更新token
	 *
	 * @param unknown_type $uid
	 * @return unknown
	 */
	function update_token($uid='')
	{
		$token = $uid . time();//token组合
		$token = md5($token);//加密
		$this->db->limit(1);
		$this->db->update('user', array('token' => $token), array('id' => $uid));
		return $token;
	}
	
	/**
	 * 更新会员信息
	 *
	 * @param unknown_type $uid
	 * @param unknown_type $data
	 */
	function update_user($uid='', $data=array())
	{
		if(empty($uid) || empty($data)) return FALSE;
		return $this->db->update('user', $data, array('id' => $uid));
	}
	
	/**
	 * 清除token
	 *
	 * @param unknown_type $uid
	 * @return unknown
	 */
	function clear_token($uid='')
	{
		return $this->db->update('user', array('token' => ''), array('id' => $uid));
	}
}


/* End of file usermodel.php */
/* Location: ./application/models/usermodel.php */